import os
def key(key_name):
    list1 = list(os.walk(os.curdir))
    list2 = []
    lentgh1 =len(list1)
    for i in range(lentgh1):
        lentgh2 = len(list1[i])
        for j in range(1,lentgh2):
            for k in list1[i][j]:
                (x,y)=os.path.splitext(k)
                if y == '.txt':
                    name =k 
                    list2.append(name)
    for 文档 in range(len(list2)):
        for 文档_1 in list2[文档]:
            f1 = open(文档_1,'r')
            f1.seek(0,0)
            line = 0
            loca = []
            for nr_line in f1:
                line +=1
                if key_name in nr_line:
                    for 次数 in range(1,(nr_line.count(key_name))+1):
                        (x,y,z)=nr_line.partition(key_name)
                        if len(x) != 0:
                            loca.append(len(x))
                        else :
                            loca.append(1)              
                        nr_line = '   '+x+z
                    print('在%s' %(list2[文档]))
                    print('关键字出现在第%d行,第%s位置'%(line,loca))
                    loca = []
key_name = str(input('请输入关键字'))
key(key_name)


            
        
